#include <bits/stdc++.h>
#include <zconf.h>
#include <fcntl.h>
#include <sys/mman.h>


using namespace std;
char num[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

string numtostring(int x) {
    string tmp = "";
    if (x == 0) {
        return "0";
    }
    while (x != 0) {
        tmp += num[x % 10];
        x /= 10;
    }
    reverse(tmp.begin(), tmp.end());
    return tmp;
}

string buff;

int main() {

    char *inputfile = "./z.out";
    char *outputfile = "./z1.out";

    char *data = NULL;
    int fd = open(inputfile, O_RDONLY);
    long size = lseek(fd, 0, SEEK_END);
    data = (char *) mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    printf("%d\n", strlen(data));
    int num[3][10] = {{123543,  65463,    435},
                      {2453423, 34352242, 33452423},
                      {235344,  2543534,  225433}};

    int t = 0;
    for (int i = 0; i < 3000000; ++i) {
        for (int j = 0; j < 3; ++j) {
            buff += numtostring(num[i%3][j]);
            buff+=',';
        }
        buff+='\n';
    }
   buff+='a';
    //buff[t]='\0';
    printf("%d\n", buff.length());
    int wfd;
    //printf("%d", buf.length());
    // 创建被写文件，如果不存在则创建，并截断文件
    wfd = open(outputfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
    // 拓展文件
    lseek(wfd, buff.length() - 1, SEEK_SET);
////    // 最后一个字节写入数据，这样才真正拓展
    write(wfd, "\0", 1);

    void *w_mem = mmap(NULL, buff.length() ,PROT_WRITE, MAP_SHARED, wfd, 0);
    memcpy(w_mem, buff.c_str(), buff.length());
    munmap(w_mem, buff.length());
    close(wfd);
    return 0;
}